package com.hsd.znsh.service.impl;

import com.hsd.znsh.entity.*;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.repository.*;
import com.hsd.znsh.service.CommonService;
import com.hsd.znsh.utils.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-01 16:59:28
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private MonitorRepository monitorRepository;

    @Autowired
    private SensorRepository sensorRepository;

    @Autowired
    private ControlRepository controlRepository;

    @Autowired
    private GatewayRepository gatewayRepository;

    @Override
    public Map getUserRole(User user) {
        Map map=new HashMap();
        map.put("userType",user.getUserType());
        map.put("username",user.getUsername());
        map.put("role", EnumUtil.getMessageByCode(user.getUserType(), UserRoleEnum.class));
        return map;
    }

    @Override
    public void updateAlias(String type, String sn, String alias) {
        if(type.equals("host")){
            Host host=hostRepository.findByHostSn(sn);
            host.setAlias(alias);
            hostRepository.save(host);
        }
        if(type.equals("sensor")){
            Sensor sensor=sensorRepository.findBySensorSn(sn);
            sensor.setAlias(alias);
            sensorRepository.save(sensor);
        }
        if(type.equals("control")){
            Control control=controlRepository.findByControlSn(sn);
            control.setAlias(alias);
            controlRepository.save(control);
        }
        if(type.equals("monitor")){
            Monitor monitor=monitorRepository.findByMonitorSn(sn);
            monitor.setAlias(alias);
            monitorRepository.save(monitor);
        }
        if(type.equals("gateway")){
            Gateway gateway=gatewayRepository.findByGatewayId(sn);
            gateway.setAlias(alias);
            gatewayRepository.save(gateway);
        }
    }
}
